package com.huaxiafinance.management.service.impl;

import com.huaxiafinance.management.constant.CommonConstant;
import com.huaxiafinance.management.dto.request.BonusDto;
import com.huaxiafinance.management.dto.response.BonusResDto;
import com.huaxiafinance.management.mapper.PartnerCommissionMapper;
import com.huaxiafinance.management.mapper.PartnerInfoMapper;
import com.huaxiafinance.management.mapper.PartnerWithdrawalsMapper;
import com.huaxiafinance.management.mapper.YfqDictionaryMapper;
import com.huaxiafinance.management.model.PartnerCommission;
import com.huaxiafinance.management.model.PartnerInfo;
import com.huaxiafinance.management.service.BonusService;
import com.huaxiafinance.management.util.DateUtil;
import com.huaxiafinance.management.util.MessageResult;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by huangwencheng on 2017/11/09.
 */
@Service("BonusService")
@SuppressWarnings("SpringJavaAutowiringInspection")
@Transactional
public class BonusServiceImpl implements BonusService {

    private static final Logger log = Logger.getLogger(BonusServiceImpl.class);

    //提现规则编号
    private String ruleCash = "1019";
    //提现月时间
    private String monthWithdrawals = "30";
    //提现周时间
    private String weekWithdrawals = "7";
    //本月
    private String nowMonth="(本月)";
    //本周
    private String nowWeek="(本周)";

    @Autowired
    private YfqDictionaryMapper yfqDictionaryMapper;

    @Autowired
    private PartnerInfoMapper partnerInfoMapper;

    @Autowired
    private PartnerWithdrawalsMapper partnerWithdrawalsMapper;

    @Autowired
    private PartnerCommissionMapper partnerCommissionMapper;

    /**
     * 查看批次列表
     *
     * @return
     */
    @Override
    public MessageResult queryBonusTypeList() throws Exception {
        MessageResult messageResult = new MessageResult();
        List<Map<String, Object>> map = yfqDictionaryMapper.selectDictList(CommonConstant.BONUS_TYPE);
        messageResult.setData(map);
        messageResult.setSuccessResult();
        return messageResult;
    }

    @Override
    public MessageResult  queryBonusList(BonusDto dto) throws Exception {
        MessageResult messageResult = new MessageResult();
        BonusResDto bonusResDto = new BonusResDto();
        PartnerInfo partnerInfo = partnerInfoMapper.queryByUserId(dto.getUserUuid());
        //判断查询提现次数
        List<Map<String, Object>> list = yfqDictionaryMapper.selectByCode(ruleCash);
        //合伙人提现情况字段
        String withdrawalsSituation = null;
        //提现时间
        String withdrawalsTime = list.get(0).get("dictRelate").toString();
        //提现次数
        Integer withdrawalsNumber = Integer.parseInt(list.get(1).get("dictRelate").toString());
        //查询合伙人提现情况
        if ((monthWithdrawals).equals(withdrawalsTime)){
           Date startTime = DateUtil.getFirstDayOfMonth();
           Date endTime = DateUtil.getLastDayOfMonth();
           int count = partnerWithdrawalsMapper.countNum(startTime,endTime,dto.getUserUuid());
            withdrawalsSituation = Integer.toString(withdrawalsNumber-count)+nowMonth;
        }else if ((weekWithdrawals).equals(withdrawalsTime)){
            Date startTime = DateUtil.getFirstDayOfWeek();
            Date endTime = DateUtil.getLastDayOfWeek();
            int count = partnerWithdrawalsMapper.countNum(startTime,endTime,dto.getUserUuid());
            withdrawalsSituation = Integer.toString(withdrawalsNumber-count)+nowWeek;
        }
        partnerInfo.setWithdrawalsSituation(withdrawalsSituation);
        bonusResDto.setPartnerInfo(partnerInfo);
        if (dto.getStartTime() != null && dto.getEndTime() != null &&
                !("").equals(dto.getStartTime()) &&
                !("").equals(dto.getEndTime())) {
            Map<String, Date> map = DateUtil.getTimeFormat(dto.getStartTime(), dto.getEndTime());
            dto.setStartDate(map.get("beginTime"));
            dto.setEndDate(map.get("overTime"));
        } else {
            dto.setStartDate(null);
            dto.setEndDate(null);
        }
        dto.setPage(dto.getPage() * dto.getRows());
        dto.setBonusType(dto.getBonusType()-1);
        List<PartnerCommission> partnerCommissionList = partnerCommissionMapper.queryByDto(dto);
        int count = partnerCommissionMapper.countByDto(dto);
        bonusResDto.setPartnerCommissionList(partnerCommissionList);
        messageResult.setData(bonusResDto);
        messageResult.setRows(count);
        messageResult.setSuccessResult();
        return messageResult;
    }


}
